import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/home/index.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import('@/views/category/index.vue'),
    meta: { title: '分类' }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('@/views/cart/index.vue'),
    meta: { title: '购物车', requiresAuth: true }
  },
  {
    path: '/my',
    name: 'My',
    component: () => import('@/views/my/index.vue'),
    meta: { title: '我的' }
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: () => import('@/views/detail/index.vue'),
    meta: { title: '商品详情' }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/search/index.vue'),
    meta: { title: '搜索' }
  },
  {
    path: '/spu-list',
    name: 'SpuList',
    component: () => import('@/views/spu-list/index.vue'),
    meta: { title: '商品列表' }
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import('@/views/order/index.vue'),
    meta: { title: '订单确认', requiresAuth: true }
  },
  {
    path: '/coupon',
    name: 'Coupon',
    component: () => import('@/views/coupon/index.vue'),
    meta: { title: '优惠券' }
  },
  {
    path: '/my-order',
    name: 'MyOrder',
    component: () => import('@/views/my-order/index.vue'),
    meta: { title: '我的订单', requiresAuth: true }
  },
  {
    path: '/my-order-center',
    name: 'MyOrderCenter',
    component: () => import('@/views/my-order-center/index.vue'),
    meta: { title: '订单中心', requiresAuth: true }
  },
  {
    path: '/my-coupon',
    name: 'MyCoupon',
    component: () => import('@/views/my-coupon/index.vue'),
    meta: { title: '我的优惠券', requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register/index.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: () => import('@/views/forgot-password/index.vue'),
    meta: { title: '忘记密码' }
  },
  {
    path: '/my-info',
    name: 'MyInfo',
    component: () => import('@/views/my-info/index.vue'),
    meta: { title: '个人信息', requiresAuth: true }
  },
  {
    path: '/order-detail/:id',
    name: 'OrderDetail',
    component: () => import('@/views/order-detail/index.vue'),
    meta: { title: '订单详情', requiresAuth: true }
  },
  {
    path: '/payment-confirm',
    name: 'PaymentConfirm',
    component: () => import('@/views/payment-confirm/index.vue'),
    meta: { title: '支付确认', requiresAuth: true }
  },
  {
    path: '/payment-detail',
    name: 'PaymentDetail',
    component: () => import('@/views/payment-detail/index.vue'),
    meta: { title: '支付详情', requiresAuth: true }
  },
  {
    path: '/test',
    name: 'Test',
    component: () => import('@/views/payment-confirm/test.vue'),
    meta: { title: 'API 测试' }
  },
  {
    path: '/points-order',
    name: 'PointsOrder',
    component: () => import('@/views/points-order/index.vue'),
    meta: { title: '积分购买', requiresAuth: true }
  },
  {
    path: '/vip-order',
    name: 'VipOrder',
    component: () => import('@/views/vip-order/index.vue'),
    meta: { title: 'VIP订单', requiresAuth: true }
  },
  {
    path: '/my-qrcode',
    name: 'MyQrcode',
    component: () => import('@/views/my-qrcode/index.vue'),
    meta: { title: '我的收款码', requiresAuth: true }
  },
  {
    path: '/my-purchases',
    name: 'MyPurchases',
    component: () => import('@/views/my-purchases/index.vue'),
    meta: { title: '我买到的', requiresAuth: true }
  },
  {
    path: '/my-purchases/detail',
    name: 'MyPurchasesDetail',
    component: () => import('@/views/my-purchases/detail.vue'),
    meta: { title: '订单详情', requiresAuth: true }
  },
  {
    path: '/points-record',
    name: 'PointsRecord',
    component: () => import('@/views/points-record/index.vue'),
    meta: { title: '积分明细', requiresAuth: true }
  },
  {
    path: '/points-record/:id',
    name: 'PointsRecordDetail',
    component: () => import('@/views/points-record/detail.vue'),
    meta: { title: '积分记录详情', requiresAuth: true }
  },
  {
    path: '/points-rules',
    name: 'PointsRules',
    component: () => import('@/views/points-record/rules.vue'),
    meta: { title: '积分规则', requiresAuth: true }
  },
  {
    path: '/sales-stats',
    name: 'SalesStats',
    component: () => import('@/views/sales-stats/index.vue'),
    meta: { title: '销售购买统计', requiresAuth: true }
  },
  {
    path: '/flow-statistics',
    name: 'FlowStatistics',
    component: () => import('@/views/flow-statistics/index.vue'),
    meta: { title: '流水统计', requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  
  // 登录验证守卫
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      // 未登录，跳转到登录页
      next('/login')
      return
    }
  }
  
  // 收款码验证守卫
  try {
    const { qrcodeGuard } = await import('./guards.js')
    await qrcodeGuard(to, from, next)
  } catch (error) {
    console.error('收款码守卫加载失败:', error)
    // 守卫加载失败时，继续正常导航
    next()
  }
})

export default router 